<?php
/**
 * This class manages profiles in this application<br>
 * Profiles in example are Bank or Consulting
 
 * @author Dennis Ahaus
 */
class ProfilesController extends AppController{

	var $uses = array('Profile');

	/**
	 * (non-PHPdoc)
	 * @see Controller::beforeFilter()
	 */
	function beforeFilter(){
		parent::beforeFilter();
	}

	/**
	 * (non-PHPdoc)
	 * @see AppController::index()
	 */
	public function index(){
		$params = array();
		if(!empty($this->params['form'])
		&& (strlen($this->params['form']['find_profile_name'])>0)
		){
			$params = array(
				'conditions' => array(
					'profile_name LIKE' => '%'.$this->params['form']['find_profile_name'].'%',
			)
			);
		}else{
			$this->params['form']['find_profile_name']='';
		}

		$this->data = $this->Profile->find('all',$params);
		if(empty($this->data)){
			$message = 'Es existieren keine Profile';
			parent::doFlash(array('Info'=>$message));
		}
	}


	/**
	 * Adds a new role
	 */
	function add(){
		if(!empty($this->data)){
			$ok = $this->Profile->save($this->data);
			$name = '"'.$this->data['Profile']['profile_name'].'"';
			if($ok){
				$message = 'Das Profil '.$name.' wurde angelegt';
				parent::doFlash(array('Info'=>$message));
				$this->redirect(array('controller'=>'profiles','action'=>'index'));
			}else{
				$message = 'Das Profil '.$name.' konnte nicht angelegt werden. ';
				$message .=$this->Session->error();
				parent::doFlash(array('Fehler'=>$message));
			}
		}


	}


	/**
	 * Updates roles data
	 * @param int $id - id of role to update data for
	 */
	function update($id=null){
		if(is_numeric($id)){
			if(!empty($this->data)){
				$this->Profile->id = $id;
				$ok = $this->Profile->save($this->data);
				$name = '"'.$this->data['Profile']['profile_name'].'"';
				if($ok){
					$message = 'Das Profil '.$name.' wurde geändert';
					parent::doFlash(array('Info'=>$message));
					$this->redirect(array('controller'=>'Profiles','action'=>'index'));
				}else{
					$message = 'Das Profil "'.$name.'" konnte nicht geändert werden. '.$this->Session->error();
					parent::doFlash(array('Fehler'=>$message));
				}
			}
			$this->data = $this->Profile->findById($id);
		}else{
			$message = 'Die übergebene '.$id.' ist nicht gültig';
			parent::doFlash(array('Fehler'=>$message));
		}
	}

	/**
	 *	Deletes a role
	 * @param int $id - id of role which should be deleted
	 */
	function delete($id=null){
		if(is_numeric($id)){
			$result = $this->Profile->findById($id);
			if(!empty($result)){
				$this->Profile->delete($id);
				$message = 'Das Profil mit der ID '.$id.' wurde gelöscht.';
				parent::doFlash(array('Info'=>$message));
				$this->redirect(array('controller'=>'profiles','action'=>'index'));
			}else{
				$message = 'Das Profil mit der ID '.$id.' existiert nicht.';
				parent::doFlash(array('Fehler'=>$message));
			}
		}else{
			$message = 'Die übergebene ID '.$id.' ist nicht gültig.';
			parent::doFlash(array('Fehler'=>$message));
		}

	}
}
?>
